package com.mgr;

import java.util.List;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.qy.DaoMgr;
import com.qy.bean.Option;
import com.qy.bean.OrderInfo;
import com.qy.util.StaticData;
import com.qy.util.TimeUtil;

/**
 * 用户订单管理工具
 * @author zwl
 * 2018年2月1日
 */
public class UserOrderMgr {
    
    //用户订单
//    private static Map<Integer,OrderInfo> userOrderMap = new ConcurrentHashMap<Integer,OrderInfo>(800);
    
    /**
     * 生成一个订单
     * @param userId
     * @param fetchPhone
     * @param fetchDate
     * @param fetchLocation
     * @param fetchCoord
     * @param returnPhone
     * @param returnDate
     * @param returnLocation
     * @param returnCoord
     * @param plateNumber
     * @param vln
     * @param channelId
     * @return
     */
    public static OrderInfo createOrder(int userId,String fetchUserName,String fetchPhone,String fetchDate,String fetchLocation,String fetchCoord,
	    String returnUserName,String returnPhone,String returnDate,String returnLocation,String returnCoord,String plateNumber,
	    String vln,int channelId,int stationId){
	OrderInfo order = new OrderInfo();
	order.setUserId(userId);
	/*
	 * 取车电话，时间，地点，坐标
	 */
	order.setFetchUserName(fetchUserName);
	order.setFetchPhone(fetchPhone);
	order.setFetchDate(TimeUtil.parseDate(fetchDate));
	order.setFetchLocation(fetchLocation);
	order.setFetchCoord(fetchCoord);
	/*
	 * 还电话，时间，地点，坐标
	 */
	order.setReturnUserName(returnUserName);
	order.setReturnPhone(returnPhone);
	order.setReturnDate(TimeUtil.parseDate(returnDate));
	order.setReturnLocation(returnLocation);
	order.setReturnCoord(returnCoord);
	/*
	 * 车牌号和车架号后6位
	 */
	order.setPlateNumber(plateNumber);
	order.setVln(vln);
	
	/*
	 * 订单状态，渠道id
	 */
	order.setOrderStatus(StaticData.OrderStatus.CREATE_NOT_PAY.getType());
	order.setChannelId(channelId);
	order.setCreateDate(TimeUtil.getSysteCurTime());
	order.setStationId(stationId);
	
	order.setOp(Option.Insert);
	DaoMgr.getOrderInfoDao().createOrderInfo(order);
//	userOrderMap.put(order.getId(), order);
	return order;
    }
    
    /**
     * 获取订单信息
     * @param orderId
     * @return
     */
    public static OrderInfo getOrderInfo(int orderId){
	OrderInfo order = null;
//	OrderInfo order = userOrderMap.get(orderId);
	if(order==null){
	    order = DaoMgr.getOrderInfoDao().getOrderInfoById(orderId);
//	    userOrderMap.put(order.getId(), order);
	}
	return order;
    }
    
    public static void update(OrderInfo orderInfo){
	DaoMgr.getOrderInfoDao().updateOrderInfo(orderInfo);
    }
    
    /**
     * 获取订单列表
     * @param userId
     */
    public static JSONArray getOrderList(int userId){
	List<OrderInfo> list =  DaoMgr.getOrderInfoDao().getOrderInfoList(userId);
	JSONArray jsa = new JSONArray();
	JSONObject js = null;
	if(list!=null&&list.size()>0){
	    for (OrderInfo tmp : list) {
		tmp = getOrderInfo(tmp.getId());
		js = new JSONObject();
		js.put("id", tmp.getId());
		js.put("createDate", TimeUtil.getDateFormat(tmp.getCreateDate()));
		js.put("fetchLocation", tmp.getFetchLocation());
		js.put("returnLocation", tmp.getReturnLocation());
		js.put("orderStatus", tmp.getOrderStatus());
		jsa.add(js);
	    }
	}
	return jsa;
    }
    /**
     * 获取订单列表
     * @param userId
     */
    public static JSONArray getOrderListByStatus(List<Integer> status){
	List<OrderInfo> list =  DaoMgr.getOrderInfoDao().getOrderInfoListByStatus(status);
	JSONArray jsa = new JSONArray();
	JSONObject js = null;
	if(list!=null&&list.size()>0){
	    for (OrderInfo tmp : list) {
		tmp = getOrderInfo(tmp.getId());
		js = new JSONObject();
		js.put("id", tmp.getId());
		js.put("createDate", TimeUtil.getDateFormat(tmp.getCreateDate()));
		js.put("fetchLocation", tmp.getFetchLocation());
		js.put("returnLocation", tmp.getReturnLocation());
		js.put("orderStatus", tmp.getOrderStatus());
		jsa.add(js);
	    }
	}
	return jsa;
    }
    /**
     * 把订单信息转换成用户前端的信息
     * @param orderInfo
     * @return
     */
    public static JSONObject OrderInfoForUser(OrderInfo orderInfo){
	JSONObject js = new JSONObject();
	js.put("fetchCoord", orderInfo.getFetchCoord());
	js.put("fetchDate",TimeUtil.getDateFormat(orderInfo.getFetchDate()) );
	js.put("fetchLocation", orderInfo.getFetchLocation());
	js.put("fetchPhone", orderInfo.getFetchPhone());
	js.put("fetchUserName", orderInfo.getFetchUserName());
	js.put("plateNumber", orderInfo.getPlateNumber());
	js.put("returnCoord", orderInfo.getReturnCoord());
	js.put("returnDate", TimeUtil.getDateFormat(orderInfo.getReturnDate()));
	js.put("returnLocation", orderInfo.getReturnLocation());
	js.put("returnPhone", orderInfo.getReturnPhone());
	js.put("returnUserName", orderInfo.getReturnUserName());
	js.put("vln", orderInfo.getVln());
	js.put("orderStatus", orderInfo.getOrderStatus());
	js.put("createDate", TimeUtil.getDateFormat(orderInfo.getCreateDate()));
	js.put("id", orderInfo.getId());
	return js;
    }
    
    /**
     * 把订单信息转换成司机取车的信息
     * @param orderInfo
     * @return
     */
    public static JSONObject OrderInfoForDriverF(OrderInfo orderInfo){
	JSONObject js = new JSONObject();
	js.put("time", TimeUtil.getDateFormat(orderInfo.getFetchDate()));
	js.put("location", orderInfo.getFetchLocation());
	js.put("phone", orderInfo.getFetchPhone());
	js.put("userName", orderInfo.getFetchUserName());
	js.put("plateNumber", orderInfo.getPlateNumber());
	js.put("status", orderInfo.getOrderStatus());
	return js;
    }
    
    /**
     * 把订单信息转换成司机送车前端的信息
     * @param orderInfo
     * @return
     */
    public static JSONObject OrderInfoForDriverR(OrderInfo orderInfo){
	JSONObject js = new JSONObject();
	js.put("time", TimeUtil.getDateFormat(orderInfo.getReturnDate()));
	js.put("location", orderInfo.getReturnLocation());
	js.put("phone", orderInfo.getReturnPhone());
	js.put("userName", orderInfo.getReturnUserName());
	js.put("plateNumber", orderInfo.getPlateNumber());
	js.put("status", orderInfo.getOrderStatus());
	return js;
    }
    /**
     * 根据订单id获取车主的id
     * @param orderId
     * @return
     */
    public static int getUserIdByOrderId(int orderId){
	int reInt = StaticData.no_param;
	OrderInfo orderInfo = getOrderInfo(orderId);
	if(orderInfo!=null){
	    reInt =  orderInfo.getUserId();
	}
	return reInt;
    }
    
    public static void save() {
//	try {
//	    List<OrderInfo> orderInfoList = new ArrayList<OrderInfo>(
//		    userOrderMap.values());
//	    for (OrderInfo info : orderInfoList) {
//		save(info);
//		if (TimeUtil.getSysCurTimeMillis() / 1000 - info.getLastUseTime() > 60 * 60) {
//		    if (info.getOp() == Option.None) {
//			info.setOp(Option.Close);
//		    } else if (info.getOp() == Option.Close) { // 清除离线玩家数据
//			// 离线超过五分钟
//			userOrderMap.remove(info.getId());
//			LogUtil.info("清除玩家Driver缓存数据:" + info.getId());
//		    }
//		}
//	    }
//	} catch (Exception e) {
//	    LogUtil.error("保存OrderInfo数据报错");
//	}

    }
}
